Bush = class(Block)

function Bush:init(id,tex,destoryTime)
    Block.init(self,id,tex,destoryTime)
    self.solidBlock = false --固体方块
    self.opacityBlock = true --透明方块
end

function Bush:render(ts,x,y,z)
    local t = ts[2]
    
    local u0 = (self.tex % 16) / 16
    local u1 = u0 + 15.99 / 256
    local v0 = self.tex // 16 / 16
    local v1 = v0 + 15.99 / 256
    
    local rots = 2
    t:brightb(240 * self:getBrightness(x,y,z))
    for r = 1,rots do
        local xa = (math.sin(r * math.pi / rots + math.pi / 4) * 0.5)
        local za = (math.cos(r * math.pi / rots + math.pi / 4) * 0.5)
        local x0 = x + 0.5 - xa
        local x1 = x + 0.5 + xa
        local y0 = y + 0.0
        local y1 = y + 1.0
        local z0 = z + 0.5 - za
        local z1 = z + 0.5 + za
    
        t:vertexUV(x0, y1, z0, u1, v1)
        t:vertexUV(x1, y1, z1, u0, v1)
        t:vertexUV(x1, y0, z1, u0, v0)
        t:vertexUV(x1, y0, z1, u0, v0)
        t:vertexUV(x0, y0, z0, u1, v0)
        t:vertexUV(x0, y1, z0, u1, v1)
    end
end

function Bush:renderInChunk(ts,x,y,z,c)
    x,z = c.xPos + x,c.zPos + z
    local t = ts[2]
    
    local u0 = (self.tex % 16) / 16
    local u1 = u0 + 15.99 / 256
    local v0 = self.tex // 16 / 16
    local v1 = v0 + 15.99 / 256
    
    local rots = 2
    t:brightb(240 * self:getBrightness(x,y,z))
    for r = 1,rots do
        local xa = (math.sin(r * math.pi / rots + math.pi / 4) * 0.5)
        local za = (math.cos(r * math.pi / rots + math.pi / 4) * 0.5)
        local x0 = x + 0.5 - xa
        local x1 = x + 0.5 + xa
        local y0 = y + 0.0
        local y1 = y + 1.0
        local z0 = z + 0.5 - za
        local z1 = z + 0.5 + za
    
        t:vertexUV(x0, y1, z0, u1, v1)
        t:vertexUV(x1, y1, z1, u0, v1)
        t:vertexUV(x1, y0, z1, u0, v0)
        t:vertexUV(x1, y0, z1, u0, v0)
        t:vertexUV(x0, y0, z0, u1, v0)
        t:vertexUV(x0, y1, z0, u1, v1)
    end
end


function Bush:tick(x,y,z)
    local below = world:getBlock(x,y - 1,z)
    if (world:getBrightness(x,y,z) < 7) or (below ~= Block.soil.id and below ~= Block.grass.id) then
        world:setBlock(x,y,z,Block.air.id)
    end
end

function Bush:neighborChanged(x,y,z,face,block)
    if face == Bottom and (block ~= Block.soil.id and block ~= Block.grass.id) then
        world:setBlock(x,y,z,Block.air.id)
        local sd = 3
        for xx = 0,sd - 1 do
            for yy = 0,sd - 1 do
                for zz = 0,sd - 1 do
                    local xp = x + (xx + 0.5) / sd
                    local yp = y + (yy + 0.5) / sd
                    local zp = z + (zz + 0.5) / sd
                    pe:add(Particle(xp,yp,zp,xp-x-0.5,yp-y-0.5,zp-z-0.5,self.tex))
                end
            end
        end
    end
end

function Block:getLayer()
    return 2
end
